// $codepro.audit.disable methodChainLength, numericLiterals, com.instantiations.assist.eclipse.analysis.unusedReturnValue, declareAsInterface, classNamingConvention
/**
 * License under GNU's GPL 
 * DoctorGui.java 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.io.*;
/**
 * Creates a GUI for the Doctor class
 * @author Team 9
 * @version 1.0
 *
 */
public class DoctorGui extends JTabbedPane {
	/**
         * 
         */
	private DefaultListModel model;
	
	/** 
	 * current doctor object
	 */
	private Doctor current;
	
    /**
     * constructor for the Doctor GUI
     * @param pat
     */
	public DoctorGui(Doctor pat) {
		current = pat;
		initialize();
	}

	private static final long serialVersionUID = 1L;
	
	public JTabbedPane tp;
	
	private JFrame frame;
	
	private JTextField textField;
	
	private JTextField textField_1;
	
	private JTextField textField_2;
	
	private JTextField textField_3;
	
	private JTextField newFName;
	
	private JTextField newLName;
	
	private JTextField newUName;
	
	private JTextField newPass;
	
	private JTextField textField_9;
	
	private JTextField textField_10;
	
	private JTextField textField_11;
	
	private JTextField textField_12;
	
	private String editL, editF, editU, editP;
	
	private JList list;
	
	private JScrollPane accountPane;
	
	private JTextPane txtpnComplaints, txtpnDiag, txtpnDo;
	
	private ArrayList<String> patient = new ArrayList<String>();
	
	private JLabel lblEditFirstName, lblEditLastName, lblEditPassword, lblEditUsername,
			lblDate, lblDoctor, lblNurse, lblTreatmentRecord;
	
	private JButton btnDelete, btnSaveRecord, btnView, btnSaveOrders, btnEdit;
	
	private JCheckBox chckbxA, chckbxB, chckbxC, chckbxD, chckbxE, chckbxA_1,
			chckbxB_1, chckbxC_1, chckbxD_1, chckbxE_1;
	
	private JPanel patients;

	private JLabel lblTotalSalary;
	
	private JLabel lblMessage;
	
	private JLabel lblMessage_1;
	
	private JLabel lblMessage_2;
	
    /**
     * Compiles a list of active users
     * @throws FileNotFoundException
     */
	// goes through all the text files and complies a list of active..bitches!
	public void sort() throws FileNotFoundException {
		Scanner fileScan = new Scanner(new File("Patient.txt"));
		fileScan.useDelimiter(" ");
		while (fileScan.hasNext()) {
			String first = fileScan.next();
			String last = fileScan.next();
			fileScan.next();
			fileScan.next();
			fileScan.nextLine();
			patient.add(last + ", " + first);
		}
		Collections.sort(patient);
	}
	
    /**
     * creates a doctor GUI
     */
	public DoctorGui() {
		initialize();
	}
	
    /**
     * the method that the constructor uses to create the GUI
     */
	public void initialize() {
		frame = new JFrame("Doctor");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setPreferredSize(new Dimension(800, 600));
		JPanel appointment = new JPanel();
		JPanel logout = new JPanel();
		JPanel main = new JPanel();
		JPanel reports = new JPanel();
		// ------------------------

		addTab("Main", main);
		main.setLayout(null);

		JLabel lblNewLabel = new JLabel("Welcome!");
		lblNewLabel.setBounds(219, 11, 75, 20);
		main.add(lblNewLabel);
		addTab("Appointments", appointment);
		appointment.setLayout(null);

		JLabel lblNewLabel_3 = new JLabel("Appointments for the day-");
		lblNewLabel_3.setBounds(309, 11, 170, 20);
		appointment.add(lblNewLabel_3);

		JTextArea txtr = new JTextArea();
		txtr.setEditable(false);
		txtr.setBounds(171, 32, 410, 429);
		for (int i = 0; i < Gui.getAppointArray().size(); i++) {
			txtr.append(Gui.getAppointArray().get(i).toString() + "\n");
		}
		appointment.add(txtr);
		addTab("Reports", reports);
		reports.setLayout(null);

		int numberOfPatients = 0;
		for (int i = 0; i < Gui.getAppointArray().size(); i++) {
			if (Gui.getAppointArray().get(i).getDoctor()
					.compareTo(current.getFirstName()) == 0) {
				numberOfPatients++;
			}
		}
		JLabel lblTotalPatientsSeen = new JLabel("Total Patients Seen: "
				+ numberOfPatients);
		lblTotalPatientsSeen.setBounds(127, 11, 184, 20);
		reports.add(lblTotalPatientsSeen);

		int sal = numberOfPatients * 100;
		lblTotalSalary = new JLabel("Total Salary: $" + sal);
		lblTotalSalary.setBounds(127, 66, 102, 20);
		reports.add(lblTotalSalary);

		// 6 main tabs
		patients = new JPanel();
		// ------------------------

		// patient panel
		patients.setLayout(null);

		JLabel lblSelectAccount = new JLabel("Select Account");
		lblSelectAccount.setBounds(10, 11, 103, 14);
		patients.add(lblSelectAccount);

		lblEditFirstName = new JLabel("Edit First Name");
		lblEditFirstName.setBounds(221, 89, 89, 14);
		patients.add(lblEditFirstName);

		lblEditLastName = new JLabel("Edit Last Name");
		lblEditLastName.setBounds(221, 114, 89, 14);
		patients.add(lblEditLastName);

		lblEditUsername = new JLabel("Edit Username");
		lblEditUsername.setBounds(221, 139, 89, 14);
		patients.add(lblEditUsername);

		lblEditPassword = new JLabel("Edit Password");
		lblEditPassword.setBounds(221, 164, 89, 14);
		patients.add(lblEditPassword);

		textField = new JTextField();
		textField.setBounds(320, 86, 86, 20);
		patients.add(textField);
		textField.setColumns(10);

		textField_1 = new JTextField();
		textField_1.setBounds(320, 111, 86, 20);
		patients.add(textField_1);
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setBounds(320, 136, 86, 20);
		patients.add(textField_2);
		textField_2.setColumns(10);

		textField_3 = new JTextField();
		textField_3.setBounds(320, 161, 86, 20);
		patients.add(textField_3);
		textField_3.setColumns(10);

		lblEditFirstName.setVisible(false);
		lblEditLastName.setVisible(false);
		lblEditUsername.setVisible(false);
		lblEditPassword.setVisible(false);
		textField.setVisible(false);
		textField_1.setVisible(false);
		textField_2.setVisible(false);
		textField_3.setVisible(false);

		btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new editListener());
		btnEdit.setBounds(221, 55, 89, 23);
		patients.add(btnEdit);

		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new saveEditListener());
		btnDelete.setBounds(317, 55, 89, 23);
		patients.add(btnDelete);

		JLabel lblNewPatient = new JLabel("New Patient");
		lblNewPatient.setBounds(221, 218, 89, 14);
		patients.add(lblNewPatient);

		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(221, 243, 89, 14);
		patients.add(lblFirstName);

		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(221, 268, 89, 14);
		patients.add(lblLastName);

		JLabel lblUserName = new JLabel("User name");
		lblUserName.setBounds(221, 293, 89, 14);
		patients.add(lblUserName);

		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(221, 318, 76, 14);
		patients.add(lblPassword);

		newFName = new JTextField();
		newFName.setBounds(320, 237, 86, 20);
		patients.add(newFName);
		newFName.setColumns(10);

		newLName = new JTextField();
		newLName.setBounds(320, 262, 86, 20);
		patients.add(newLName);
		newLName.setColumns(10);

		newUName = new JTextField();
		newUName.setBounds(320, 287, 86, 20);
		patients.add(newUName);
		newUName.setColumns(10);

		newPass = new JTextField();
		newPass.setBounds(320, 312, 86, 20);
		patients.add(newPass);
		newPass.setColumns(10);

		JButton btnCreate = new JButton("Create");
		btnCreate.addActionListener(new addListener());

		btnCreate.setBounds(317, 343, 89, 23);
		patients.add(btnCreate);

		addTab("Patients", patients);

		txtpnDo = new JTextPane();
		txtpnDo.setText(" ");
		txtpnDo.setBounds(416, 444, 353, 40);
		patients.add(txtpnDo);

		JLabel lblDoctorsOrder = new JLabel("Doctor's Orders-");
		lblDoctorsOrder.setBounds(416, 293, 108, 14);
		patients.add(lblDoctorsOrder);

		lblTreatmentRecord = new JLabel(
				"Treatment Record For Appointment on: date");
		lblTreatmentRecord.setBounds(416, 8, 400, 14);
		patients.add(lblTreatmentRecord);
		try {
			sort();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		list = new JList(patient.toArray());
		accountPane = new JScrollPane(list);
		accountPane.setBounds(10, 36, 201, 482);
		patients.add(accountPane);

		// btnSaveRecord = new JButton("Edit Records");
		// btnSaveRecord.setVisible(false);
		// btnSaveRecord.addActionListener(new eRecordsListener());
		// btnSaveRecord.setBounds(666, 259, 103, 23);
		// patients.add(btnSaveRecord);

		btnSaveOrders = new JButton("Edit Records & Orders");
		btnSaveOrders.setBounds(575, 495, 200, 23);
		btnSaveOrders.addActionListener(new eRecordsListener());
		btnSaveOrders.setVisible(false);
		patients.add(btnSaveOrders);
		lblDoctor = new JLabel("Doctor:");
		lblDoctor.setBounds(416, 33, 300, 14);
		patients.add(lblDoctor);
		lblNurse = new JLabel("Nurse:");
		lblNurse.setBounds(416, 61, 300, 14);
		patients.add(lblNurse);
		// lblDate = new JLabel("date");
		// lblDate.setBounds(665, 8, 100, 14);
		// patients.add(lblDate);
		txtpnComplaints = new JTextPane();
		txtpnComplaints.setText(" ");
		txtpnComplaints.setEditable(false);
		txtpnComplaints.setBounds(416, 159, 353, 40);
		patients.add(txtpnComplaints);
		JLabel lblChiefComplaints = new JLabel("Chief Complaints:");
		lblChiefComplaints.setBounds(416, 140, 136, 14);
		patients.add(lblChiefComplaints);
		JLabel lblDiagnosis = new JLabel("Diagnosis:");
		lblDiagnosis.setBounds(416, 210, 89, 14);
		patients.add(lblDiagnosis);
		txtpnDiag = new JTextPane();
		txtpnDiag.setText(" ");
		txtpnDiag.setEditable(false);
		txtpnDiag.setBounds(416, 230, 353, 40);
		patients.add(txtpnDiag);
		JLabel lblBP = new JLabel("BP:");
		lblBP.setBounds(416, 86, 46, 14);
		patients.add(lblBP);
		JLabel lblHeight = new JLabel("Height:");
		lblHeight.setBounds(416, 109, 46, 14);
		patients.add(lblHeight);
		JLabel lblWeight = new JLabel("Weight:");
		lblWeight.setBounds(573, 86, 50, 14);
		lblWeight.setBounds(573, 86, 76, 14);
		patients.add(lblWeight);
		JLabel lblTempurature = new JLabel("Tempurature:");
		lblTempurature.setBounds(573, 109, 75, 14);
		lblTempurature.setBounds(573, 109, 100, 14);
		patients.add(lblTempurature);
		textField_9 = new JTextField();
		textField_9.setBounds(466, 83, 86, 20);
		patients.add(textField_9);
		textField_9.setColumns(10);
		textField_10 = new JTextField();
		textField_10.setBounds(466, 111, 86, 20);
		patients.add(textField_10);
		textField_10.setColumns(10);
		textField_11 = new JTextField();
		textField_11.setBounds(683, 85, 86, 20);
		patients.add(textField_11);
		textField_11.setColumns(10);
		textField_12 = new JTextField();
		textField_12.setBounds(683, 108, 86, 20);
		patients.add(textField_12);
		textField_12.setColumns(10);
		textField_9.setEditable(false);
		textField_10.setEditable(false);
		textField_11.setEditable(false);
		textField_12.setEditable(false);
		JLabel lblLabWork = new JLabel("Lab Work:");
		lblLabWork.setBounds(416, 318, 89, 14);
		patients.add(lblLabWork);
		chckbxA = new JCheckBox("Blood Test");
		chckbxA.setBounds(476, 314, 101, 23);
		patients.add(chckbxA);
		chckbxB = new JCheckBox("X-Ray");
		chckbxB.setBounds(573, 314, 97, 23);
		patients.add(chckbxB);
		chckbxC = new JCheckBox("MRI");
		chckbxC.setBounds(672, 314, 97, 23);
		patients.add(chckbxC);
		chckbxD = new JCheckBox("Throat Culture");
		chckbxD.setBounds(476, 343, 140, 23);
		patients.add(chckbxD);
		chckbxE = new JCheckBox("Lumbar Puncture");
		chckbxE.setBounds(618, 343, 177, 23);
		patients.add(chckbxE);
		JLabel lblPrescriptions = new JLabel("Prescriptions:");
		lblPrescriptions.setBounds(416, 371, 108, 14);
		patients.add(lblPrescriptions);
		chckbxA_1 = new JCheckBox("Vicodin");
		chckbxA_1.setBounds(665, 392, 97, 23);
		patients.add(chckbxA_1);
		chckbxB_1 = new JCheckBox("Zyrtec");
		chckbxB_1.setBounds(573, 367, 97, 23);
		patients.add(chckbxB_1);
		chckbxC_1 = new JCheckBox("Prozac");
		chckbxC_1.setBounds(666, 369, 97, 23);
		patients.add(chckbxC_1);
		chckbxD_1 = new JCheckBox("Ibuprofen");
		chckbxD_1.setBounds(476, 392, 97, 23);
		patients.add(chckbxD_1);
		chckbxE_1 = new JCheckBox("Keflex");
		chckbxE_1.setBounds(573, 393, 97, 23);
		patients.add(chckbxE_1);
		JLabel lblFollowUpInstructions = new JLabel("Follow Up Instructions");
		lblFollowUpInstructions.setBounds(416, 428, 183, 14);
		patients.add(lblFollowUpInstructions);

		chckbxA.setEnabled(false);
		chckbxB.setEnabled(false);
		chckbxC.setEnabled(false);
		chckbxD.setEnabled(false);
		chckbxE.setEnabled(false);
		chckbxA_1.setEnabled(false);
		chckbxB_1.setEnabled(false);
		chckbxC_1.setEnabled(false);
		chckbxD_1.setEnabled(false);
		chckbxE_1.setEnabled(false);
		txtpnDo.setEditable(false);

		btnView = new JButton("View");
		btnView.addActionListener(new viewListener());
		btnView.setBounds(272, 29, 89, 23);
		patients.add(btnView);

		lblMessage = new JLabel("Message 1");
		lblMessage.setBounds(221, 189, 140, 14);
		patients.add(lblMessage);

		lblMessage_1 = new JLabel("Message 2");
		lblMessage_1.setBounds(221, 347, 46, 14);
		patients.add(lblMessage_1);

		lblMessage_2 = new JLabel("Message 3");
		lblMessage_2.setBounds(416, 495, 161, 14);
		patients.add(lblMessage_2);
		addTab("Logout", logout);

		JLabel lblNewLabel_1 = new JLabel("Are You Sure?");
		logout.add(lblNewLabel_1);

		JButton btnNewButton = new JButton("Yes");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Gui obj = new Gui();
				obj.getFrame().setVisible(true);
				setVisible(false);
				frame.setVisible(false);
				removeAll();
			}
		});
		logout.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("No");
		btnNewButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				setSelectedIndex(0);
			}
		});
		logout.add(btnNewButton_1);

		frame.getContentPane().add(this);
		frame.pack();
		frame.setVisible(true);
	}
	
    /**
     * returns the frame the GUI is in    
     * @return the jframe the GUI is in
     */
	public JFrame getFrame() {
		return frame;
	}
	
    /**
     * the main method that runs the GUI
     * @param args the command line arguments
     */
	public static void main(String[] args) {
		DoctorGui doctor = new DoctorGui();
		doctor.getFrame();
	}
	
    /**
     * An ActionListener for editing patients info
     * @author Team 9
     *
     */
	private class editListener implements ActionListener {
		@Override
		public String toString() {
			return "editListener []";
		}

		/**
		 * The action performed method to edit patient info
		 */
		public void actionPerformed(ActionEvent event) {

			String la = (String) list.getSelectedValue();
			boolean yes;
			if (la == null) {
				System.out.println("Please Select a user!");
				yes = false;
			} else {
				yes = true;
			}

			if (yes) {
				btnView.setVisible(false);
				lblEditFirstName.setVisible(true);
				lblEditLastName.setVisible(true);
				lblEditUsername.setVisible(true);
				lblEditPassword.setVisible(true);
				textField.setVisible(true);
				textField_1.setVisible(true);
				textField_2.setVisible(true);
				textField_3.setVisible(true);
				btnDelete.setText("Save Edits");
				String[] tokens = la.split(", ");
				editL = tokens[0];
				editF = tokens[1]; // first and last name of the field selected
				Scanner fileScan = null;
				try {
					fileScan = new Scanner(new File("Patient.txt"));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				while (fileScan.hasNext()) {
					String first = fileScan.next();
					String last = fileScan.next();
					if (editF.equals(first) && editL.equals(last)) {
						textField.setText(first);
						textField_1.setText(last);
						String un = fileScan.next();
						editU = un;
						textField_2.setText(un);
						String pw = fileScan.next();
						editP = pw;
						textField_3.setText(pw);
					}
					fileScan.nextLine();
				}
			}
		}
	}
	
    /**
     * the Action Listener to save user data
     * @author Team 9
     *
     */
	// edit patient user name pass stuff
	private class saveEditListener implements ActionListener {
		@Override
		public String toString() {
			return "saveEditListener []";
		}

		/**
		 * the action performed method that edits users
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (btnDelete.getText() == "Save Edits") {
				btnView.setVisible(true);
				File temp = new File("temp.txt");
				FileWriter fstream = null;
				try {
					fstream = new FileWriter("temp.txt");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BufferedWriter out = new BufferedWriter(fstream);
				BufferedReader reader = null;
				try {
					File inputFile = new File("Patient.txt");
					reader = new BufferedReader(new FileReader(inputFile));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String currentLine;
				Scanner fileScan = null;
				try {
					fileScan = new Scanner(new File("Patient.txt"));
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				boolean create = true;
				if (!(textField_2.getText().equals(editU))) {
					try {
						create = taken(textField_2.getText());
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				if (!create) {
					System.out
							.println("User name is taken, please pick another one.");
				} else {
					System.out.println("Patient Edited!");
					lblEditFirstName.setVisible(false);
					lblEditLastName.setVisible(false);
					lblEditUsername.setVisible(false);
					lblEditPassword.setVisible(false);
					textField.setVisible(false);
					textField_1.setVisible(false);
					textField_2.setVisible(false);
					textField_3.setVisible(false);
					btnDelete.setText("Delete");
				}

				try {
					while ((currentLine = reader.readLine()) != null) {
						String first = fileScan.next();
						String last = fileScan.next();
						fileScan.nextLine();
						if (!editF.equals(first) && !editL.equals(last)) {
							try {
								out.write(currentLine);
								out.write("\n");
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				if (create) {
					try {
						out.write(textField.getText().trim() + " "
								+ textField_1.getText().trim() + " "
								+ textField_2.getText().trim() + " "
								+ textField_3.getText().trim() + " 1 \n");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else {
					try {
						out.write(editF + " " + editL + " " + editU + " "
								+ editP);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				File la = new File("Patient.txt");
				la.delete();
				File ba = new File("Patient.txt");
				temp.renameTo(ba);
			} else if (btnDelete.getText() == "Delete") {
				String la = (String) list.getSelectedValue();
				boolean yes;
				if (la == null) {
					System.out.println("Please Select a user!");
					yes = false;
				} else {
					yes = true;
				}
				if (yes) {
					String[] tokens = la.split(", ");
					editL = tokens[0];
					editF = tokens[1]; // first and last name of the field
										// selected
					File temp = new File("temp.txt");
					FileWriter fstream = null;
					try {
						fstream = new FileWriter("temp.txt");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					BufferedWriter out = new BufferedWriter(fstream);
					BufferedReader reader = null;
					try {
						File inputFile = new File("Patient.txt");
						reader = new BufferedReader(new FileReader(inputFile));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					String currentLine;
					Scanner fileScan = null;
					try {
						fileScan = new Scanner(new File("Patient.txt"));
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					try {
						while ((currentLine = reader.readLine()) != null) {
							String first = fileScan.next();
							String last = fileScan.next();
							fileScan.nextLine();
							// System.out.println(first + " " + last);
							if (!editF.equals(first) && !editL.equals(last)) {
								try {
									out.write(currentLine);
									out.write("\n");
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						}
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					try {
						out.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					File la1 = new File("Patient.txt");
					la1.delete();
					File ba = new File("Patient" + ".txt");
					temp.renameTo(ba);
					patient = new ArrayList<String>();
					try {
						sort();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					list.setListData(patient.toArray());
					accountPane.updateUI();
				}
			}
		}
	}
	
    /**
     * states whether a username is already taken
     * @param userName the patient username
     * @return true or false if it is
     * @throws FileNotFoundException
     */
	public boolean taken(String userName) throws FileNotFoundException {
		String tUName;
		String[] types = { "Patient", "Doctor", "Admin", "Nurse" };
		for (int i = 0; i < 4; i++) {
			Scanner fileScan = new Scanner(new File(types[i] + ".txt"));
			while (fileScan.hasNext()) {
				fileScan.next();
				fileScan.next();
				tUName = fileScan.next();
				fileScan.nextLine();
				if (userName.equals(tUName)) {
					return false;
				}
			}
		}
		return true;
	}
	
    /**
     * The action listener that adds a user
     * @author Team 9
     *
     */
	private class addListener implements ActionListener {
		@Override
		public String toString() {
			return "addListener []";
		}

		/**
		 * the action performed method that the GUI uses to add users
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			boolean full = true;
			if (newFName.getText().length() == 0
					|| newLName.getText().length() == 0
					|| newUName.getText().length() == 0
					|| newPass.getText().length() == 0) {
				System.out
						.println("Please make sure all fields are completed!");
				full = false;
			} else {
				// message.setText(" ");
			}

			boolean create = false;
			try {
				create = taken(newUName.getText());
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			if (!create) {
				System.out
						.println("User name is taken, please pick another one.");
			} else {
				// tRMessage.setText(" ");
			}

			if (create) {
				String fName = newFName.getText().trim();
				String lName = newLName.getText().trim();
				String uName = newUName.getText().trim();
				String passW = newPass.getText().trim();

				// figure out who is logged in....
				String file = lName + ", " + fName + ".txt";
				File pFile = new File("Patient/" + file);
				try {
					pFile.createNewFile();
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				new Patient(fName, lName, uName, passW);
				try {
					// Create file
					FileWriter fstream = new FileWriter("Patient.txt", true);
					BufferedWriter out = new BufferedWriter(fstream);
					out.write(fName + " " + lName + " " + uName + " " + passW
							+ " 1" + " \n");
					// Close the output stream
					out.close();
				} catch (Exception e) {// Catch exception if any
					System.err.println("Error: " + e.getMessage());
				}

				patient.add(lName + ", " + fName);

				newFName.setText(" ");
				newLName.setText(" ");
				newUName.setText(" ");
				newPass.setText(" ");
			}
			Collections.sort(patient);
			list.setListData(patient.toArray());
			accountPane.updateUI();
		}
	}
	
    /**
     * Action Listener that allows you to views users 
     * @author Team 9
     *
     */
	private class viewListener implements ActionListener {
       	@Override
		public String toString() {
			return "viewListener []";
		}

		/**
    	 * the action performed method that the GUI uses to view users
    	 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (btnView.getText().equals("View")) {
				String la = (String) list.getSelectedValue();
				boolean yes;
				if (la == null) {
					lblMessage_2.setText("Please Select a user!");
					yes = false;
				} else {
					yes = true;
				}

				if (yes) {
					// btnSaveRecord.setVisible(true);
					// btnSaveOrders.setVisible(true);
					Scanner fileScan = null;
					String[] tokens = la.split(", ");
					editL = tokens[0];
					editF = tokens[1]; // first and last name of the field
										// selected
					try {
						fileScan = new Scanner(new File("Patient/" + editL
								+ ", " + editF + ".txt"));
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					String he = fileScan.nextLine();
					if (he.equals("Treatment Record for Appointment on: ")) {
						lblTreatmentRecord.setText("No Appointment on Record!");
					} else {
						btnView.setText("Done");
						btnEdit.setVisible(false);
						btnDelete.setVisible(false);
						btnSaveOrders.setVisible(true);
						lblTreatmentRecord.setText(he);
						// fileScan.nextLine();
						lblDoctor.setText(fileScan.nextLine());
						lblNurse.setText(fileScan.nextLine());
						textField_9.setText(fileScan.nextLine());
						textField_10.setText(fileScan.nextLine());
						textField_11.setText(fileScan.nextLine());
						textField_12.setText(fileScan.nextLine());
						txtpnComplaints.setText(fileScan.nextLine());
						txtpnDiag.setText(fileScan.nextLine());
						txtpnDo.setText(fileScan.nextLine());
						JCheckBox[] checkBox = { chckbxA, chckbxB, chckbxC,
								chckbxD, chckbxE, chckbxA_1, chckbxB_1,
								chckbxC_1, chckbxD_1, chckbxE_1 };
						for (int i = 0; i < checkBox.length; i++) {
							checkBox[i].setSelected(fileScan.nextBoolean());
						}
					}
				}
			} else if (btnView.getText().equals("Done")) {
				btnView.setText("View");
				clear();
			}
		}
	}
	
    /**
     * ERecords Action Listener class
     * @author Team 9
     *
     */
	private class eRecordsListener implements ActionListener {
    	@Override
		public String toString() {
			return "eRecordsListener []";
		}

		/**
    	 * ERecords action Performed method
    	 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (btnSaveOrders.getText().equals("Edit Records & Orders")) {
				textField_9.setEditable(true);
				textField_10.setEditable(true);
				textField_11.setEditable(true);
				textField_12.setEditable(true);
				txtpnComplaints.setEditable(true);
				txtpnDiag.setEditable(true);
				JCheckBox[] checkBox = { chckbxA, chckbxB, chckbxC, chckbxD,
						chckbxE, chckbxA_1, chckbxB_1, chckbxC_1, chckbxD_1,
						chckbxE_1 };
				for (int i = 0; i < checkBox.length; i++) {
					checkBox[i].setEnabled(true);
				}
				txtpnDo.setEditable(true);
				btnSaveOrders.setText("Save Records & Orders");
			} else if (btnSaveOrders.getText().equals("Save Records & Orders")) {
				// tring file = editL + ", " + editF + ".txt";
				FileWriter fstream = null;
				File temp = new File("Patient/temp.txt");
				try {
					fstream = new FileWriter("Patient/temp.txt", true);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BufferedWriter out = new BufferedWriter(fstream);
				try {
					out.write(lblTreatmentRecord.getText() + " \n");
					out.write(lblDoctor.getText() + " \n");
					out.write(lblNurse.getText() + " \n");
					out.write(textField_9.getText() + " \n");
					out.write(textField_10.getText() + " \n");
					out.write(textField_11.getText() + " \n");
					out.write(textField_12.getText() + " \n");
					out.write(txtpnComplaints.getText() + " \n");
					out.write(txtpnDiag.getText() + " \n");
					out.write(txtpnDo.getText() + " \n");
					out.write(chckbxA.isSelected() + " \n");
					out.write(chckbxB.isSelected() + " \n");
					out.write(chckbxC.isSelected() + " \n");
					out.write(chckbxD.isSelected() + " \n");
					out.write(chckbxE.isSelected() + " \n");
					out.write(chckbxA_1.isSelected() + " \n");
					out.write(chckbxB_1.isSelected() + " \n");
					out.write(chckbxC_1.isSelected() + " \n");
					out.write(chckbxD_1.isSelected() + " \n");
					out.write(chckbxE_1.isSelected() + " \n");
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				String file = editL + ", " + editF + ".txt";
				File la = new File("Patient/" + file);
				la.delete();
				File ba = new File("Patient/" + file);
				temp.renameTo(ba);
				clear();
			}
		}
	}
	
/**
 * clears the labels
 */
	void clear() {
		lblTreatmentRecord.setText("Treatment Record for Appointment on: date");
		lblDoctor.setText("Doctor: ");
		lblNurse.setText("Nurse: ");
		textField_9.setText(" ");
		textField_10.setText(" ");
		textField_11.setText(" ");
		textField_12.setText(" ");
		txtpnComplaints.setText(" ");
		txtpnDiag.setText(" ");
		txtpnDo.setText(" ");
		JCheckBox[] checkBox = { chckbxA, chckbxB, chckbxC, chckbxD, chckbxE,
				chckbxA_1, chckbxB_1, chckbxC_1, chckbxD_1, chckbxE_1 };
		for (int i = 0; i < checkBox.length; i++) {
			checkBox[i].setEnabled(false);
			checkBox[i].setSelected(false);
		}
		btnSaveOrders.setText("Edit Records & Orders");
		btnSaveOrders.setVisible(false);
		btnView.setText("View");
		btnEdit.setVisible(true);
		btnDelete.setVisible(true);
	}
}